﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;

namespace Qr.Servce {
    public class HttpUtility {

        public static string Get(string url) {
            HttpWebRequest request = WebRequest.CreateHttp(url);
            string jsonString = "";
            request.Method = "GET";
            HttpWebResponse response = null;

            response = request.GetResponse() as HttpWebResponse;
            StreamReader reader =
            new StreamReader(response.GetResponseStream());

            jsonString = reader.ReadToEnd();
            reader.Dispose();

            return jsonString;
        }

        public static string Post(string url, string json) {
            HttpWebRequest request = WebRequest.CreateHttp(url);
            request.Method = "POST";
            request.ContentType = "application/json";
            Stream requestStream = request.GetRequestStream();
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(json);
            requestStream.Write(buffer, 0, buffer.Length);
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            StreamReader reader =
                new StreamReader(response.GetResponseStream());

            string jsonString = reader.ReadToEnd();
            reader.Dispose();
            return jsonString;
        }


        public static string Post(string url, string fileName, string contentType, byte[] file, NameValueCollection nvc = null) {
            string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.ContentType = "multipart/form-data; boundary=" + boundary;
            request.Method = "POST";
            request.KeepAlive = true;
            request.Credentials = System.Net.CredentialCache.DefaultCredentials;

            Stream ms = new System.IO.MemoryStream();
            byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

            if (nvc != null) {
                string formdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";
                foreach (string key in nvc.Keys) {
                    string formitem = string.Format(formdataTemplate, key, nvc[key]);
                    byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
                    ms.Write(formitembytes, 0, formitembytes.Length);
                }
            }
            ms.Write(boundarybytes, 0, boundarybytes.Length);

            string headerTemplate = "Content-Disposition: form-data; name=\"media\"; filename=\"{0}\"\r\n Content-Type: {1}\r\n\r\n";
            string header = string.Format(headerTemplate, fileName, contentType);
            byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
            ms.Write(headerbytes, 0, headerbytes.Length);
            ms.Write(file, 0, (int)file.Length);
            ms.Write(boundarybytes, 0, boundarybytes.Length);

            request.ContentLength = ms.Length;

            Stream requestStream = request.GetRequestStream();

            ms.Position = 0;
            byte[] tempBuffer = new byte[ms.Length];
            ms.Read(tempBuffer, 0, tempBuffer.Length);
            ms.Close();
            requestStream.Write(tempBuffer, 0, tempBuffer.Length);
            requestStream.Close();

            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            string json = reader.ReadToEnd();
            reader.Dispose();
            return json;
        }



    }
}